package en.kaist.cs408.acav;

import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.widget.Toast;

public class WifiBroadcastReceiver extends BroadcastReceiver {

	private WifiManager wifiManager;
	private WifiAdapter wifiAdapter;
	private List<WifiItem> listeWifiItem;

	@Override
	public void onReceive(Context context, Intent intent) {
		wifiManager = ((CheckVoice) context).getCurrentWifiManager();
		wifiAdapter = ((CheckVoice) context).getWifiAdapter();
		listeWifiItem = ((CheckVoice) context).getListeWifiItem();

		// Check if the object is correctly instantiated
		if (wifiManager != null) {

			// Check if the wifi is turned on
			if (wifiManager.isWifiEnabled()) {
				// Scans are gathered
				List<ScanResult> listeScan = wifiManager.getScanResults();
				 

				// Clear the list
				listeWifiItem.clear();

				// For each scan
				for (ScanResult scanResult : listeScan) {
					WifiItem item = new WifiItem();
					

					item.setAdresseMac(scanResult.BSSID);
					item.setAPName(scanResult.SSID);
					item.setForceSignal(scanResult.level);

					Log.d("FormationWifi", scanResult.SSID + " LEVEL "
							+ scanResult.level + "capabilities" + scanResult.capabilities);

					listeWifiItem.add(item);
				}

				// refresh the list
				wifiAdapter.notifyDataSetChanged();
			} else {
				Toast.makeText(context, "You may turn on your wifi",
						Toast.LENGTH_SHORT).show();
			}
		}

	}

}
